<?php
/**
 * 积分服务
 * 
 * 提供积分获取、积分设置等服务
 * 
 * @author thinksns
 *
 */
class CreditService extends Service {
	
	/**
	 * 查询当前登陆用户的积分分页数据。
	 * @param $stype		查看的积分类型	'gain'--获得的积分  'used'--已使用的积分
	 */
	public function search4User($stype='gain',$limit=15){
		if($stype!='gain'&&$stype!='used')return null;
		$where['user_id']=$_SESSION['mid'];
		$where['pay_state']='pay';
		$where['order_state']='N';
		$where['enable']='Y';
		if($stype=='gain')$where['gain_credit']=array('gt',0);
		if($stype=='used')$where['used_credit']=array('gt',0);
		$order='ctime desc';
		$orderdao = M('Order');
		$pagedata = $orderdao->kPageQuery($where,$order,$limit);
		if(!empty($pagedata)&&!empty($pagedata['data'])){
			if($stype=='gain') {
				foreach ($pagedata['data'] as $vo){
					$creditdata[]=array(	'mode'=>'gain'//记录积分获得方式
											,'credit'=>$vo['gain_credit']//对应积分
											,'gtime'=>$vo['pay_time']//对应积分
										);
				}
			}
			if($stype=='used') {
				foreach ($pagedata['data'] as $vo){
					$creditdata[]=array(	'mode'=>'used'//记录积分获得方式
											,'credit'=>$vo['used_credit']//对应积分
											,'gtime'=>$vo['pay_time']//对应积分
										);
				}
			}
			$pagedata['data']=$creditdata;
		}
		return $pagedata;
	}
	
	
	/**
	 * 查询当前登陆使用的积分。
	 */
	public function count4UserTotalUsed(){
		$where['user_id']=$_SESSION['mid'];
		$where['pay_state']='pay';
		$where['order_state']='N';
		$where['enable']='Y';
		$where['used_credit']=array('gt',0);
		$orderdao = M('Order');
		$result =	$orderdao->where($where)->limit(1)->field('sum(`used_credit`) as totalcredit')->find();
		$totalcredit	=	$result['totalcredit'];
		return $totalcredit;
	}
	
	
	/**
	 * 查询当前登陆使用的积分。
	 */
	public function count4UserTotalGain(){
		$where['user_id']=$_SESSION['mid'];
		$where['pay_state']='pay';
		$where['order_state']='N';
		$where['enable']='Y';
		$where['gain_credit']=array('gt',0);
		$orderdao = M('Order');
		$result =	$orderdao->where($where)->limit(1)->field('sum(`gain_credit`) as totalcredit')->find();
		$totalcredit	=	$result['totalcredit'];
		return $totalcredit;
	}
	
	
	//服务初始化
	public function init() {
	}

	//运行服务，系统服务自动运行
	public function run() {
	}
	
	/* 后台管理相关方法 */

	//启动服务，未编码
	public function _start(){
		return true;
	}
	
	//停止服务，未编码
	public function _stop(){
		return true;
	}

	//卸载服务，未编码
	public function _install(){
		return true;
	}

	//卸载服务，未编码
	public function _uninstall(){
		return true;
	}
}
?>